package com.ehotting.mqbd.core.util;

import com.ehotting.eaf.core.StringHelper;
import com.ehotting.eaf.core.utils.NumberHelper;
import com.ehotting.mqbd.api.MQDBConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AgentIdGenUtil {

    private static Logger logger = LoggerFactory.getLogger(AgentIdGenUtil.class);

    public static String genAgentId(String module,Long userId,String projectId){
        if(StringHelper.isBlank(projectId)){
            projectId = "0";
        }
        return module+ MQDBConstants.COLO+userId+MQDBConstants.LINE+projectId;
    }


    public static String genDefaultAgentId(String module){
        return module+MQDBConstants.COLO+"0"+MQDBConstants.LINE+"0";
    }

    public static String getModule(String agentId){
        logger.info("agentId:{}",agentId);
        String[] tempArr = agentId.split(MQDBConstants.COLO);
        if(tempArr.length<2) {
            logger.error("Error agentId[{}],return null.",agentId);
            return null;
        }
        String module = tempArr[0];
        return module;
    }



    public static Long getUserId(String agentId){
        logger.info("agentId:{}",agentId);
        String[] tempArr = agentId.split(MQDBConstants.COLO);
        if(tempArr.length<2) {
            logger.error("Error agentId[{}],return 0.",agentId);
            return 0L;
        }
        agentId = tempArr[1];
        if(StringHelper.hasText(agentId) && agentId.indexOf(MQDBConstants.LINE)>0){
            String[] strArr = agentId.split(MQDBConstants.LINE);
            return NumberHelper.parseLong(strArr[0]);
        }
        logger.info("userId error,return 0.");
        return NumberHelper.parseLong(agentId);
    }

    public static Long getProjectId(String agentId){
        logger.info("agentId:{}",agentId);
        String[] tempArr = agentId.split(MQDBConstants.COLO);
        if(tempArr.length<2) {
            logger.error("Error agentId[{}],return 0.",agentId);
            return 0L;
        }
        agentId = tempArr[1];
        if(StringHelper.hasText(agentId) && agentId.indexOf(MQDBConstants.LINE)>0){
            String[] strArr = agentId.split(MQDBConstants.LINE);
            return NumberHelper.parseLong(strArr[1]);
        }
        logger.info("projectId error,return 0.");
        return 0L;
    }



}
